package com.welike.shibo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.welike.shibo.eneity.SysPermission;
import com.welike.shibo.mapper.SysPermissionMapper;
import com.welike.shibo.service.SysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * Created by fangyu on 2019/11/21.
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService{

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public JSONObject getUserPermission(String username) {
        return getUserPermissionFromDB(username);
    }

    public JSONObject getUserPermissionFromDB(String username){
        JSONObject userPermission = sysPermissionMapper.getUserPermission(username);
        //管理员角色ID为1
        int adminRoleId = 1;
        String roleIdKey = "roleId";
        //如果是管理员
        if (adminRoleId == userPermission.getIntValue(roleIdKey)) {
            //查询所有菜单  所有权限
            Set<String> menuList = sysPermissionMapper.getAllMenu();
            Set<String> permissionList = sysPermissionMapper.getAllPermission();
            userPermission.put("menuList", menuList);
            userPermission.put("permissionList", permissionList);
        }
        return userPermission;
    }
}
